import java.util.LinkedList;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-03-13
 * Time: 20:27
 */
public class TestDemo {
    public static void main(String[] args) {
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.addLast(1);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(4);
        singleLinkedList.addLast(5);
        singleLinkedList.myToString();
        singleLinkedList.pintList(singleLinkedList.head);




    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) {
            return null;
        }
        ListNode pl = headA;//指向长的链表
        ListNode ps = headB;//指向短的链表
        //1、分别求长度
        int lenA = 0;
        int lenB = 0;
        while (pl != null) {
            lenA++;
            pl = pl.next;
        }

        while (ps != null) {
            lenB++;
            ps = ps.next;
        }
        //需要重新修改指向
        pl = headA;
        ps = headB;

        //2、已经知道两个链表的长度了
        int len = lenA-lenB;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //1、求长度    2、pl永远指向 最长的那个链表   ps永远指向最短的那个链表

        //3、就是让最长的链表走len步
            while(len>0){
                pl=pl.next;
                len--;
            }
        //4、一人一步走 直到相遇
            while(pl!=ps){
                pl=pl.next;
                ps=ps.next;
            }

        //5、是不是两个都是null-》没有相交
        if(pl==null){
            return null;
        }
        return pl;
    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode newNode=new ListNode(-1);
        ListNode tmp=newNode;
        while (list1!=null&&list2!=null){
            if(list1.val<list2.val){
                tmp.next=list1;
                tmp=tmp.next;
                list1=list1.next;
            }else {
                tmp.next=list2;
                tmp=tmp.next;
                list2=list2.next;
            }

        }
        if(list1!=null){
            tmp.next=list1;
        }
        if(list2!=null){
            tmp.next=list2;
        }
        return newNode.next;
    }

    public static void main3(String[] args) {
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.addLast(11);
        singleLinkedList.addLast(11);
        singleLinkedList.addLast(11);
        singleLinkedList.myToString();
        singleLinkedList.deleteDuplicates();
        singleLinkedList.myToString();

    }
    public static void main1(String[] args) {
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.createList();
        System.out.println(singleLinkedList.hasCycle());
        //System.out.println(singleLinkedList.detectCycle().val);
    }

    public static void main2(String[] args) {
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.addLast(11);
        singleLinkedList.addLast(8);
        singleLinkedList.addLast(5);
        singleLinkedList.addLast(13);
        singleLinkedList.addLast(2);
        singleLinkedList.myToString();
        singleLinkedList.partition(10);
        singleLinkedList.myToString();
    }




}
